package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.Ticket;
import com.bestcem.xm.ticket.entity.mongo.TicketFlow;
import com.bestcem.xm.ticket.entity.mongo.TicketTemplate;
import com.bestcem.xm.ticket.entity.mongo.TicketTemplateV2;
import com.bestcem.xm.ticket.entity.mysql.TicketDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/24 11:13
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketConverter {

    @Nullable
    public static Ticket doToDto(TicketDO ticketDO) {
        if (Objects.isNull(ticketDO)) {
            return null;
        }

        Ticket ticket = new Ticket();
        ticket.setId(ticketDO.getTicketId());
        ticket.setName(ticketDO.getName());
        ticket.setSysId(ticketDO.getSysId());
        ticket.setOrgId(ticketDO.getOrgId());
        ticket.setTicketNo(ticketDO.getTicketNo());
        ticket.setSurveyId(ticketDO.getSurveyId());
        ticket.setSurveyTitle(ticketDO.getSurveyTitle());
        ticket.setFollowRoleId(ticketDO.getFollowRoleId());
        ticket.setFollowUserIds(CommonConverter.stringToList(ticketDO.getFollowUserIds(), String.class));
        ticket.setAuditUserIds(CommonConverter.stringToList(ticketDO.getAuditUserIds(), String.class));
        ticket.setAuditRoleId(ticketDO.getAuditRoleId());
        ticket.setCurrentFollowNode(ticketDO.getCurrentFollowNode());
        ticket.setFollowNodeCount(ticketDO.getFollowNodeCount());
        ticket.setCurrentAuditNode(ticketDO.getCurrentAuditNode());
        ticket.setAuditNodeCount(ticketDO.getAuditNodeCount());
        ticket.setRegion(ticketDO.getRegion());
        ticket.setRegionName(ticketDO.getRegionName());
        ticket.setAlertId(ticketDO.getAlertId());
        ticket.setAlertRuleIds(CommonConverter.stringToList(ticketDO.getAlertRuleIds(), String.class));
        ticket.setResId(ticketDO.getResId());
        ticket.setContact(CommonConverter.stringToObject(ticketDO.getContact(), Ticket.Contact.class));
        ticket.setStatus(ticketDO.getStatus());
        ticket.setHasFollowOverdue(ticketDO.getHasFollowOverdue());
        ticket.setFollowDeadline(ticketDO.getFollowDeadline());
        ticket.setHasAuditOverdue(ticketDO.getHasAuditOverdue());
        ticket.setAuditDeadline(ticketDO.getAuditDeadline());
        ticket.setTicketDeadline(ticketDO.getTicketDeadline());
        ticket.setTicketConfigId(ticketDO.getTicketConfigId());
        ticket.setTicketFlow(CommonConverter.stringToObject(ticketDO.getTicketFlow(), TicketFlow.class));
        ticket.setTicketTemplate(CommonConverter.stringToObject(ticketDO.getTicketTemplate(), TicketTemplate.class));
        ticket.setTicketTemplateV2(CommonConverter.stringToObject(ticketDO.getTicketTemplateV2(), TicketTemplateV2.class));
        ticket.setTicketRecordIds(CommonConverter.stringToList(ticketDO.getTicketRecordIds(), String.class));
        ticket.setClosedDate(ticketDO.getCloseTime());
        ticket.setTicketFlowList(CommonConverter.stringToList(ticketDO.getTicketFlowList(), String.class));
        ticket.setTicketFinishTime(ticketDO.getTicketFinishTime());
        ticket.setTicketFailedTime(ticketDO.getTicketFailedTime());
        ticket.setCurFollowUserId(CommonConverter.stringToList(ticketDO.getCurFollowUserId(), String.class));
        ticket.setAlertRecordId(ticketDO.getAlertRecordId());
        ticket.setTemplateDetailFields(CommonConverter
                .stringToList(ticketDO.getTemplateDetailFields(), Ticket.TemplateDetailField.class));
        ticket.setNeedRecord(ticketDO.getNeedRecord());
        ticket.setType(ticketDO.getType());
        ticket.setHasCommit(ticketDO.getHasCommit());
        ticket.setMemberId(ticketDO.getMemberId());
        ticket.setDetailTemplateId(ticketDO.getDetailTemplateId());
        ticket.setHandleTemplateId(ticketDO.getHandleTemplateId());
        ticket.setOperationType(ticketDO.getOperationType());
        ticket.setCreatedName(ticketDO.getCreatorName());
        ticket.setSample(ticketDO.getSample());
        if (StringUtils.isNotEmpty(ticketDO.getTriggerNames())) {
            ticket.setTriggerNames(new LinkedList<>(CommonConverter.stringToList(ticketDO.getTriggerNames(), String.class)));
        }
        CommonConverter.setEntity(ticket, ticketDO);
        return ticket;
    }

    @Nonnull
    public static List<Ticket> doListToDtoList(List<TicketDO> ticketDOList) {
        List<Ticket> list = new ArrayList<>();
        if (Objects.isNull(ticketDOList)) {
            return list;
        }

        for (TicketDO ticketDO : ticketDOList) {
            Ticket ticket = doToDto(ticketDO);
            list.add(ticket);
        }
        return list;
    }

    @Nullable
    public static TicketDO dtoToDo(Ticket ticket) {
        if (Objects.isNull(ticket)) {
            return null;
        }

        TicketDO ticketDO = new TicketDO();
        ticketDO.setTicketId(ticket.getId());
        ticketDO.setName(ticket.getName());
        ticketDO.setSysId(ticket.getSysId());
        ticketDO.setOrgId(ticket.getOrgId());
        ticketDO.setTicketNo(ticket.getTicketNo());
        ticketDO.setSurveyId(ticket.getSurveyId());
        ticketDO.setSurveyTitle(ticket.getSurveyTitle());
        ticketDO.setFollowRoleId(ticket.getFollowRoleId());
        ticketDO.setFollowUserIds(CommonConverter.listToString(ticket.getFollowUserIds()));
        ticketDO.setAuditUserIds(CommonConverter.listToString(ticket.getAuditUserIds()));
        ticketDO.setAuditRoleId(ticket.getAuditRoleId());
        ticketDO.setCurrentFollowNode(ticket.getCurrentFollowNode());
        ticketDO.setFollowNodeCount(ticket.getFollowNodeCount());
        ticketDO.setCurrentAuditNode(ticket.getCurrentAuditNode());
        ticketDO.setAuditNodeCount(ticket.getAuditNodeCount());
        ticketDO.setRegion(ticket.getRegion());
        ticketDO.setRegionName(ticket.getRegionName());
        ticketDO.setAlertId(ticket.getAlertId());
        ticketDO.setAlertRuleIds(CommonConverter.listToString(ticket.getAlertRuleIds()));
        ticketDO.setResId(ticket.getResId());
        ticketDO.setContact(CommonConverter.objectToString(ticket.getContact()));
        ticketDO.setStatus(ticket.getStatus());
        ticketDO.setHasFollowOverdue(ticket.getHasFollowOverdue());
        ticketDO.setFollowDeadline(ticket.getFollowDeadline());
        ticketDO.setHasAuditOverdue(ticket.getHasAuditOverdue());
        ticketDO.setAuditDeadline(ticket.getAuditDeadline());
        ticketDO.setTicketDeadline(ticket.getTicketDeadline());
        ticketDO.setTicketConfigId(ticket.getTicketConfigId());
        ticketDO.setTicketFlow(CommonConverter.objectToString(ticket.getTicketFlow()));
        ticketDO.setTicketTemplate(CommonConverter.objectToString(ticket.getTicketTemplate()));
        ticketDO.setTicketTemplateV2(CommonConverter.objectToString(ticket.getTicketTemplateV2()));
        ticketDO.setTicketRecordIds(CommonConverter.listToString(ticket.getTicketRecordIds()));
        ticketDO.setCloseTime(ticket.getClosedDate());
        ticketDO.setTicketFlowList(CommonConverter.listToString(ticket.getTicketFlowList()));
        ticketDO.setTicketFinishTime(ticket.getTicketFinishTime());
        ticketDO.setTicketFailedTime(ticket.getTicketFailedTime());
        ticketDO.setCurFollowUserId(CommonConverter.listToString(ticket.getCurFollowUserId()));
        ticketDO.setAlertRecordId(ticket.getAlertRecordId());
        ticketDO.setTemplateDetailFields(CommonConverter.listToString(ticket.getTemplateDetailFields()));
        ticketDO.setNeedRecord(ticket.getNeedRecord());
        ticketDO.setType(ticket.getType());
        ticketDO.setHasCommit(ticket.getHasCommit());
        ticketDO.setMemberId(ticket.getMemberId());
        ticketDO.setDetailTemplateId(ticket.getDetailTemplateId());
        ticketDO.setHandleTemplateId(ticket.getHandleTemplateId());
        ticketDO.setOperationType(ticket.getOperationType());
        ticketDO.setCreatorName(ticket.getCreatedName());
        ticketDO.setSample(ticket.getSample());
        // 触发条件名称
        ticketDO.setTriggerNames(CommonConverter.listToString(ticket.getTriggerNames()));

        CommonConverter.setDo(ticket, ticketDO);
        return ticketDO;
    }
}
